/**
 * Copyright 2008 Frank van Lingen Licensed under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package superpodder.controller.feedreader;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

import org.gnu.stealthp.rsslib.RSSItem;

public class RssLib4JComparator implements Comparator {

    public int compare(Object ev1, Object ev2) {
        Date e1 = this.convertDate(((RSSItem) ev1).getDate());
        Date e2 = this.convertDate(((RSSItem) ev2).getDate());
        
        
        if ((e1 == null) || (e2 == null)){
            System.out.println("Problem with finding date format");
            return -1;
        }

        if (e1.after(e2)) {
            return -1;
        }
        if (e1.before(e2)) {
            return 1;
        }
        return 0;
        
    }
    
    public Date convertDate(String dateStr){
        // feeds can have different data formats.
        // we try to deal with the most common ones.
        SimpleDateFormat format1 = new SimpleDateFormat(
                "EEE, dd MMM yyyy HH:mm zzz");
        SimpleDateFormat format2 = new SimpleDateFormat(
        "EEE, dd MMM yyyy HH:mm:ss zzz");
        SimpleDateFormat format3 = new SimpleDateFormat(
        "dd MMM yyyy HH:mm:ss zzz");
        SimpleDateFormat format4 = new SimpleDateFormat(
        "dd MMM yyyy K:mm:ss zzz");
        
        
        Date e = null;
        
        boolean parseit = true;
        
        if (parseit){
        try {
            e = format1.parse(dateStr);
            parseit = false;
        } catch (final Exception ex) {
            //System.out.println("Problem with parsing date format 1: "+dateStr);
        }
        }
        if (parseit){
            try {
                e = format2.parse(dateStr);
                parseit = false;
            } catch (final Exception ex) {
               // System.out.println("Problem with parsing date format 2: "+dateStr);
            }
            }
        if (parseit){
            try {
                e = format3.parse(dateStr);
                parseit = false;
            } catch (final Exception ex) {
             //   System.out.println("Problem with parsing date format 3: "+dateStr);
            }
            }        
        if (parseit){
            try {
                e = format4.parse(dateStr);
                parseit = false;
            } catch (final Exception ex) {
               // System.out.println("Problem with parsing date format 4: "+dateStr);
                ex.printStackTrace();
                e = null;
            }
            }
        return e;
    }

}
